package com.dean.action;

import com.dean.entity.DeanDbConnInfo;
import com.dean.entity.DeanImportInfo;
import com.dean.entity.DeanResponse;
import com.dean.entity.SysDbConnInfo;
import com.dean.exception.DeanRuntimeException;
import com.dean.service.ApacheCsvOprService;
import com.dean.service.CsvOptService;
import com.dean.service.ExcelOptService;
import com.dean.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

/**
 * @ClassName ExcelOptAction
 * @Description excel 操作控制器
 * @Author dean
 * @Date 2021/11/16 10:09
 */
public class CsvOptAction {

    private final Logger logger = LoggerFactory.getLogger(CsvOptAction.class);

    private static volatile CsvOptAction INSTAMCE;

    private ApacheCsvOprService csvOptService;

    private CsvOptAction(){
        this.csvOptService = ApacheCsvOprService.getInstance();
    }
    // 双重锁检查 单例模式
    public static CsvOptAction getInstance(){
        if(INSTAMCE == null){
            synchronized (CsvOptAction.class){
                if(INSTAMCE == null){
                    INSTAMCE = new CsvOptAction();
                }
            }
        }
        return INSTAMCE;
    }



    /**
     * excel export
     * @param dbConnectInfo
     * @param filePath
     * @param sqls
     * @return
     */
    public DeanResponse<String> exportCsv(SysDbConnInfo dbConnectInfo, String filePath, String [] sqls){
       return exportCsv(dbConnectInfo,filePath,sqls,null);
    }


    /**
     * excel export
     * @param dbConnectInfo
     * @param filePath
     * @param sqls
     * @return
     */
    public DeanResponse<String> exportCsv(SysDbConnInfo dbConnectInfo, String filePath, String [] sqls,String delimiter){
        try {
            csvOptService.csvExport(dbConnectInfo,filePath,sqls,delimiter);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error();
            }
        }
        return DeanResponse.ok("操作成功");
    }

    /**
     * excel 导入  根据提供的模板 直接查找目录下的所有excel文件和sheet内容 能匹配上所有的表头就导入
     * @param connectInfo
     * @param filePath
     * @param importInfo
     * @return
     */
    public DeanResponse importCsv(SysDbConnInfo connectInfo, String filePath, DeanImportInfo importInfo){
        try {
            List<File> csvFiles = FileUtils.getCsvFiles(filePath);
            csvOptService.importCsv(connectInfo,csvFiles,importInfo);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error();
            }
        }
        return DeanResponse.ok("操作成功");
    }
}
